exports.update = (req,res) => {

    var bodyParams = req.routeInfo.bodyParams;

    //获取主键ID
    var d_id = bodyParams['d_id'];
    delete bodyParams['d_id'];

    //获取参数数据
    var params = bodyParams['d_params'];
    delete bodyParams['d_params'];

    //更新文档信息
    function updateDocs(cb){

        //组装更新语句
        var updateStr = "set ";
		for(var key in bodyParams){
			updateStr += key + "='" + bodyParams[key] + "',";
		}

		updateStr = updateStr.substring(0,updateStr.length - 1);
        var sql = "update docs " + updateStr + " where d_id = " + d_id;
        
        sqliteDb.run(sql,function(error,data){

            if(error){
                return RES.errorResponse(res,"抱歉，文档更新失败");
            }

            cb();


        });

    }

    //新增文档参数
    function updateDocsParams(cb){

        //判断是否需要更新参数信息
        if(params == undefined){
            return cb();
        }

        //删除当前文档的所有参数
        var sql = "delete from docs_request_parameter where drp_did = " + d_id;

        sqliteDb.run(sql,function(error,data){

            if(error){
                return RES.errorResponse(res,"抱歉，文档更新失败");
            }

            cb();


        });

        
    }

    //重新写入参数信息
    function addParams(cb){

        //每个循环的处理
        function taskFun(item,index,callback){

            var sql = `insert into docs_request_parameter values(NULL,
                '${d_id}',
                '${item.drp_name}',
                '${item.drp_is_must}',
                '${item.drp_rule}',
                '${item.drp_note}',
                '${item.drp_way}')`;
            
            sqliteDb.run(sql,function(error,data){    

                callback();
            });

        }

        //创建循环
        F.SerialEach(params,taskFun,cb);

    }

    F.Serial([updateDocs,updateDocsParams,addParams],function(){
        return RES.successResponse(res);
    });


 }